import 'package:flutter/material.dart';
import '../models/program_block.dart';

/// 程序块的可视化组件
class ProgramBlockWidget extends StatefulWidget {
  final ProgramBlock block;
  final bool isDraggable;
  final Function(ProgramBlock)? onTap;
  final Function(ProgramBlock)? onDragCompleted;
  
  const ProgramBlockWidget({
    super.key,
    required this.block,
    this.isDraggable = true,
    this.onTap,
    this.onDragCompleted,
  });

  @override
  State<ProgramBlockWidget> createState() => _ProgramBlockWidgetState();
}

class _ProgramBlockWidgetState extends State<ProgramBlockWidget> {
  @override
  Widget build(BuildContext context) {
    final block = widget.block;
    
    // 构建块的内容
    Widget blockContent = Container(
      margin: const EdgeInsets.symmetric(vertical: 4.0),
      decoration: BoxDecoration(
        color: block.color,
        borderRadius: BorderRadius.circular(8.0),
        boxShadow: [
          BoxShadow(
            color: Colors.black.withOpacity(0.2),
            blurRadius: 3.0,
            offset: const Offset(0, 2),
          ),
        ],
      ),
      child: Material(
        color: Colors.transparent,
        child: InkWell(
          onTap: () {
            if (widget.onTap != null) {
              widget.onTap!(block);
            }
          },
          borderRadius: BorderRadius.circular(8.0),
          child: Padding(
            padding: const EdgeInsets.all(12.0),
            child: Row(
              children: [
                Icon(block.icon, color: Colors.white),
                const SizedBox(width: 8.0),
                Expanded(
                  child: Text(
                    block.name,
                    style: const TextStyle(
                      color: Colors.white,
                      fontWeight: FontWeight.bold,
                    ),
                  ),
                ),
                if (block.parameters.isNotEmpty) ..._buildParameters(block),
              ],
            ),
          ),
        ),
      ),
    );
    
    // 如果块是可拖动的，包装在Draggable中
    if (widget.isDraggable) {
      return Draggable<ProgramBlock>(
        data: block.clone(),
        feedback: SizedBox(
          width: 250,
          child: Opacity(
            opacity: 0.7,
            child: blockContent,
          ),
        ),
        childWhenDragging: Opacity(
          opacity: 0.3,
          child: blockContent,
        ),
        onDragCompleted: () {
          if (widget.onDragCompleted != null) {
            widget.onDragCompleted!(block);
          }
        },
        child: blockContent,
      );
    }
    
    return blockContent;
  }
  
  // 构建参数输入部分
  List<Widget> _buildParameters(ProgramBlock block) {
    final List<Widget> paramWidgets = [];
    
    for (var param in block.parameters) {
      paramWidgets.add(
        Container(
          margin: const EdgeInsets.only(left: 8.0),
          padding: const EdgeInsets.symmetric(horizontal: 8.0, vertical: 4.0),
          decoration: BoxDecoration(
            color: Colors.white.withOpacity(0.3),
            borderRadius: BorderRadius.circular(4.0),
          ),
          child: Row(
            mainAxisSize: MainAxisSize.min,
            children: [
              Text(
                '$param: ',
                style: const TextStyle(
                  color: Colors.white,
                  fontSize: 12.0,
                ),
              ),
              GestureDetector(
                onTap: () => _showParameterEditDialog(block, param),
                child: Text(
                  '${block.parameterValues[param]}',
                  style: const TextStyle(
                    color: Colors.white,
                    fontWeight: FontWeight.bold,
                    fontSize: 12.0,
                  ),
                ),
              ),
            ],
          ),
        ),
      );
    }
    
    return paramWidgets;
  }
  
  // 显示参数编辑对话框
  void _showParameterEditDialog(ProgramBlock block, String param) {
    final TextEditingController controller = TextEditingController(
      text: block.parameterValues[param].toString(),
    );
    
    showDialog(
      context: context,
      builder: (context) => AlertDialog(
        title: Text('编辑 $param'),
        content: TextField(
          controller: controller,
          keyboardType: TextInputType.number,
          decoration: InputDecoration(
            labelText: param,
            border: const OutlineInputBorder(),
          ),
        ),
        actions: [
          TextButton(
            onPressed: () => Navigator.pop(context),
            child: const Text('取消'),
          ),
          TextButton(
            onPressed: () {
              setState(() {
                // 尝试将输入转换为数字
                final value = double.tryParse(controller.text);
                if (value != null) {
                  block.parameterValues[param] = value;
                }
              });
              Navigator.pop(context);
            },
            child: const Text('确定'),
          ),
        ],
      ),
    );
  }
} 